<template>
    <div class="code">
        <div class="avatar">
            <van-image
                round
                width="60px"
                height="60px"
                :src="userinfo.linkmanAvatar"
            />
            <div class="username">{{ userinfo.name }}</div>
        </div>
        <div class="code-item">
            <div class="codema">
                <van-image
                    @longpress="bankImg"
                    width="100%"
                    height="100%"
                    :src="codeUrl"
                />
                <!--创建一个画布，将它移出屏幕外看不到的地方-->
                <canvas class="canvas-code" canvas-id="myQrcode" style="background:#fff;width: 200px;height: 200px; display:block; left:-800rpx;position:absolute;"/>
            </div>
            <div class="text">
                长按保存二维码
            </div>
        </div>
    </div>
</template>

<script>
import QRCode from '../../../utils/qrcode';
export default {
    name: "code",
    data() {
        return {
            userinfo: {
                linkmanAvatar: '',
                name: '',
                usrShopId: ''
            },
            ewmImg: '',
            codeUrl: ''
        }
    },
    // onShow() {
    //     this.userinfo = uni.getStorageSync('userinfo')
    // },
    onShow() {
        this.userinfo = uni.getStorageSync('userinfo')
        console.log(this.userinfo)
        const that = this
        new QRCode('myQrcode',{
            text: `https://www.1cardd.com/heka/c/?shopId=${this.userinfo.usrShopId}&shopName=${this.userinfo.usrShopName}`,
            width: 141,	//canvas 画布的宽
            height: 141,	//canvas 画布的高
            padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
            correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
            callback: (res) => {
                //工具回调数据
                // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
                wx.hideLoading()
                console.log("生成二维码",res)
                //将图片路劲放入data中，显示在wxml的image标签上
                // that.setData({
                //     ewmImg:res.path,
                //     showEwmIndex:e.currentTarget.dataset.index
                // })
                that.codeUrl = res.path
            }
        })
    },
    methods: {
        //长按图片
        bankImg(){
            uni.showActionSheet({
                itemList: ['保存图片'],
                success: (res) => {
                    if(res.tapIndex == 0){
                        uni.saveImageToPhotosAlbum({
                            filePath: this.codeUrl,//    图片文件路径，可以是临时文件路径也可以是永久文件路径，不支持网络图片路径
                            success: () => {
                                uni.showToast({
                                    title: '保存成功',
                                    duration: 2000
                                });
                            },
                            fail: () => {
                                uni.showToast({
                                    title: '保存失败',
                                    duration: 2000
                                });
                            }
                        });
                    }
                },
                fail: function (res) {
                    console.log(res.errMsg);
                }
            });
        }
    }
    
}
</script>

<style lang="less">
.code {
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    background-color: #FBFBFB;
    .avatar {
        width: 100%;
        padding: 16px 36px;
        display: flex;
        align-items: center;
        .username {
            margin-left: 12px;
            font-size: 16px;
            font-weight: bold;
        }
    }
    .code-item {
        background-color: #FFFFFF;
        border-radius: 15px;
        margin-top: 6px;
        padding: 40px 32px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .codema {
            width: 160px;
            height: 160px;
        }
        .text {
            color: #999999;
            font-size: 14px;
            text-align: center;
            line-height: 40px;
            margin-top: 20px;
        }
    }
}
</style>